package pers.liu.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.liu.entity.User;
import pers.liu.service.UserService;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiuGuangShuo
 * @since 2024年11月13日
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询所有数据
     * get http://localhost:8080/user
     */
    @GetMapping
    public List<User> list(){
        return userService.list();
    }

    /**
     * 增加用户
     * post http://localhost:8080/user
     */
    @PostMapping
    public boolean save(@RequestBody User user){
        return userService.save(user);
    }

    /**
     * 修改用户
     * put http://localhost:8080/user
     */
    @PutMapping
    public boolean update(@RequestBody User user){
        return userService.updateById(user);
    }

    /**
     * 删除用户
     * delete http://localhost:8080/user/?
     */
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id){
        return userService.removeById(id);
    }

    /**
     * 主键查询
     * get http://localhost:8080/user/?
     */
    @GetMapping("/{id}")
    public User byId(@PathVariable Long id){
        return userService.getById(id);
    }

    /**
     * 登录验证
     */
    @PostMapping("/checkLogin")
    public User checkLogin(@RequestBody User user){
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("username",user.getUsername());
        qw.eq("password",user.getPassword());

        List<User> list=userService.list(qw);
        if(list.size()>0){
            return list.get(0).setPassword(null);//脱敏处理，隐藏密码
        }else{
            return null;
        }
    }
}

